jQuery.fn.jpage = function(config){
	 var pagerContainer=this.get(0)
     var pagerTable=this.get(1);
     var pagerBar=this.get(2);
     init("#"+pagerContainer.id,"#"+pagerTable.id,"#"+pagerBar.id,config);
     function init(t_container,t_table,t_bar,config){
          //公有变量
          if(!t_container) return;
          if(!t_table)return;
          if(!t_bar)return;
          
          var dataStore = config.dataStore; //数据
          var totalRecord = 50; //设置默认总记录数          
          var configPage = config.perPage > 0 ? config.perPage : 15;
          var perPage = configPage;//每页显示记录数
          var proxyUrl = config.proxyUrl != null ? config.proxyUrl : 'dataproxy.jsp';//数据代理地址
          var groupSize = config.groupSize; //组大小
          var ajaxParam = config.ajaxParam;//ajax的请求参数
          var showMode = config.showMode == null || config.showMode == '' ? 'full' : config.showMode;   //显示模式
          var themeName = config.themeName == null || config.themeName == '' ? 'black' : config.themeName; //主题名称
          var cols=config.cols;//表格列属性名
          var picCol=config.picCol;//显示图片的列信息
          var isView=config.isView!=null?config.isView:false; //是否显示详细链接
          var keyField=config.keyField;//列表主键
          var hasCheck=config.hasCheck!=null?config.hasCheck:false;//是否出现复选框
          
          //私有变量
          var totalPage = Math.ceil(totalRecord/perPage);                                                                                                                         //总页数
          var currentPage = 1;//当前页码
          var startRecord;                                                                                                                                                                                        //每页起始记录
          var endRecord;                                                                                                                                                                                          //每页结束记录
          var gpStartPage;                                                                                                                                                                                        //每页起始记录，相对于当前组
          var gpEndPage;                                                                                                                                                                                          //每页结束记录，相对于当前组
          var gpStartRecord = 1;                                                                                                                                                                          //组开始记录
          var gpEndRecord;  
          
          //*******************工具条内容 开始***************************************************************/
          var toolbar = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="'+themeName+'_pgToolbar"><tr><td>';
          toolbar += '<table border="0" cellspadding="0" cellspacing="0" class="'+themeName+'_pgPanel"><tr>';
          if(showMode == 'full'){
                  toolbar += '<td valign="middle"><select class="'+themeName+'_pgPerPage" title="每页显示条数">';
                  if(config.perPage>0)
                          toolbar += '<option value="'+config.perPage+'">'+config.perPage+'</option>';
                  toolbar += '<option value="5">5</option>';
                  toolbar += '<option value="10">10</option>';
                  toolbar += '<option value="15">15</option>';
                  toolbar += '<option value="20">20</option>';
                  toolbar += '<option value="25">25</option>';
                  toolbar += '<option value="40">40</option>';
                  toolbar += '</select>&nbsp;</td>';
                  toolbar += '<td valign="middle"><div class="'+themeName+'_separator"></div></td>';
          }
          toolbar += '<td valign="middle"><div class="'+themeName+'_pgBtn '+themeName+'_pgFirst" title="首页"></div></td>';
          toolbar += '<td valign="middle"><div class="'+themeName+'_pgBtn '+themeName+'_pgPrev" title="上页"></div></td>';
          if(showMode == 'full'){
                  toolbar += '<td valign="middle" width="10" align="left"><div class="'+themeName+'_separator"></div></td>';
                  toolbar += '<td valign="middle">第 <input class="'+themeName+'_pgCurrentPage" type="text" value="' + currentPage + '" title="页码" /> 页 / 共 <span class="'+themeName+'_pgTotalPage">' + totalPage + '</span> 页</td>';
                  toolbar += '<td valign="middle" width="10" align="right"><div class="'+themeName+'_separator"></div></td>';
          }else if(showMode == 'normal'){
                  toolbar += '<td valign="middle"><div class="'+themeName+'_separator"></div></td>';
                  toolbar += '<td valign="middle"> <input class="'+themeName+'_pgCurrentPage" type="text" value="' + currentPage + '" title="页码" /> /  <span class="'+themeName+'_pgTotalPage">' + totalPage + '</span> 页</td>';
                  toolbar += '<td valign="middle"><div class="'+themeName+'_separator"></div></td>';
          }
          toolbar += '<td valign="middle"><div class="'+themeName+'_pgBtn '+themeName+'_pgNext" title="下页"></div></td>';
          toolbar += '<td valign="middle"><div class="'+themeName+'_pgBtn '+themeName+'_pgLast" title="尾页"></div></td>';
          if(groupSize){
                  toolbar += '<td valign="middle"><div class="'+themeName+'_separator"></div></td>';
                  toolbar += '<td valign="middle"><div class="'+themeName+'_pgBtn '+themeName+'_pgRefresh" title="刷新"></div></td>';
          }
          if(showMode == 'full'){
                  toolbar += '<td valign="middle" width="10" align="left"><div class="'+themeName+'_separator"></div></td>';
                  toolbar += '<td valign="middle" class="'+themeName+'_pgSearchInfo">检索到&nbsp;' + totalRecord + '&nbsp;条记录，显示第&nbsp;<span class="'+themeName+'_pgStartRecord">' + startRecord + '</span>&nbsp;条&nbsp;-&nbsp;第&nbsp;<span class="'+themeName+'_pgEndRecord">' + endRecord + '</span>&nbsp;条记录</td>';
          }
          toolbar += '</tr></table>';
          toolbar += '</td></tr></table>';
 		  //*******************工具条内容 结束***************************************************************/
          if(configPage<totalRecord){                 
                $(t_bar).html(toolbar);
          }
          //输出空白行
          var strBlankTr=getBlankTr(perPage);
          $(t_table).html(strBlankTr);               
   
          var btnRefresh = $(t_bar+" ."+themeName+"_pgRefresh");                                                                                                                      //刷新按钮
          var btnNext =$(t_bar+" ."+themeName+"_pgNext");                                                                                                                                     //下一页按钮
          var btnPrev = $(t_bar+" ."+themeName+"_pgPrev");                                                                                                                                    //上一页按钮
          var btnFirst = $(t_bar+" ."+themeName+"_pgFirst");                                                                                                                          //首页按钮
          var btnLast = $(t_bar+" ."+themeName+"_pgLast");                                                                                                                                    //末页按钮
          var btnGo = $(t_bar+" ."+themeName+"_pgNext,"+t_bar+" ."+themeName+"_pgLast");
          var btnBack = $(t_bar+" ."+themeName+"_pgPrev,"+t_bar+" ."+themeName+"_pgFirst");
          var btn = $(t_bar+" ."+themeName+"_pgFirst,"+t_bar+" ."+themeName+"_pgPrev,"+t_bar+" ."+themeName+"_pgNext,"+t_bar+" ."+themeName+"_pgLast");
          var mask;
         
          var valCurrentPage = $(t_bar+" ."+themeName+"_pgCurrentPage");
          var valStartRecord = $(t_bar+" ."+themeName+"_pgStartRecord");
          var valEndRecord =$(t_bar+" ."+themeName+"_pgEndRecord");
          var valContainer = $(t_table);
          var valPerPage = $(t_bar+" ."+themeName+"_pgPerPage");
          var valTotalPage = $(t_bar+" ."+themeName+"_pgTotalPage");
          $(t_bar+" ."+themeName+"_pgPerPage").attr("value",perPage);
          getGroupStartEnd();
          getStartEnd();
          if(dataStore==null || ( (groupSize!=null) && (perPage!=configPage) ) )
                  getRemoteData();
          else{
                  getStartEnd();
                  loadData();
                  refresh();
          } 
          //刷新按钮监听
          btnRefresh.bind("mousedown",pressHandler).bind("mouseup",unpressHandler).bind("mouseout",unpressHandler); 
          //刷新工具栏
          refresh();         
          //按钮监听
          btnNext.click(
               function(){
                   if(currentPage < totalPage){
                           if(!dataStore || currentPage == gpEndPage){
                                   currentPage += 1;
                                   getGroupStartEnd();
                                   getStartEnd();
                                   getRemoteData();
                           }else{
                                   currentPage += 1;
                                   getStartEnd();
                                   loadData();
                                   refresh();
                           }
                   }
               }
          );      
          btnPrev.click(
               function(){
                  if(currentPage > 1){
                          if(!dataStore || currentPage == gpStartPage){
                                  currentPage -= 1;
                                  getGroupStartEnd();
                                  getStartEnd();
                                  getRemoteData();
                          }else{
                                  currentPage -= 1;
                                  getStartEnd();
                                  loadData();
                                  refresh();
                          }
                  }
               }
          );
          btnFirst.click(
              function(){
                if(!dataStore || currentPage > 1){
                        if(gpStartPage > 1){
                                currentPage = 1;
                                getGroupStartEnd();
                                getStartEnd();
                                getRemoteData();
                        }else{
                                currentPage = 1;
                                getStartEnd();
                                loadData();
                                refresh();
                        }
                }
              }
          );
          btnLast.click(
              function(){
                 if(!dataStore || currentPage < totalPage){
                         if(gpEndPage > 0 && gpEndPage < totalPage){
                                 currentPage = totalPage;
                                 getGroupStartEnd();
                                 getStartEnd();
                                 getRemoteData();
                         }else{
                                 currentPage = totalPage;
                                 getStartEnd();
                                 loadData();
                                 refresh();
                         }
                 }
              }
          );
          btnRefresh.click(
              function(){
                 getGroupStartEnd();
                 getStartEnd();
                 getRemoteData();
              }
          );
         
          //页码输入框监听
          valCurrentPage.keydown(
              function(event){
                  var targetPage = parseInt($(this).val());
                  if(event.keyCode==13 && targetPage>=1 && targetPage<=totalPage){
                          if(!dataStore || gpStartPage > targetPage || (gpEndPage > 0 && gpEndPage < targetPage)){
                                  currentPage = targetPage;
                                  getGroupStartEnd();
                                  getStartEnd();
                                  getRemoteData();
                          }else{
                                  currentPage = targetPage;
                                  getStartEnd();
                                  loadData();
                                  refresh();
                          }
                  }
              }
          );         
          valPerPage.change(
                  function(){
                          perPage = parseInt($(this).val());
                          currentPage = 1;
                          totalPage = Math.ceil(totalRecord/perPage);
                          if(groupSize){
                                  getGroupStartEnd();
                                  getStartEnd();
                                  getRemoteData();
                          }else{
                                  getStartEnd();
                                  loadData();
                                  refresh();                              
                          }                      
                  }
          );         
          /*********************************init私有函数***************************************************/
          //置为正在检索状态
          function startLoad(){
                  $(t_container).addClass(themeName+"_container");
                  mask = document.createElement('div');
                  $(mask).addClass(themeName+"_mask");
                  $(mask).css("height",$(t_container).height());
                  $(t_container).append(mask);
                  $(t_bar+" ."+themeName+"_pgRefresh").addClass(themeName+"_pgLoad");
                  $(t_bar+" ."+themeName+"_pgSearchInfo").html("正在检索中，请稍后...");
          }
         //置为结束检索状态
          function overLoad(){
                  $(t_bar+" ."+themeName+"_pgRefresh").removeClass(themeName+"_pgLoad");
                  $(t_bar+" ."+themeName+"_pgTotalPage").html(totalPage);//增加——设置总页数                         
                  $(t_bar+" ."+themeName+"_pgSearchInfo").html('检索到&nbsp;' + totalRecord + '&nbsp;条记录，显示第&nbsp;<span class="'+themeName+'_pgStartRecord">' + (startRecord+gpStartRecord-1) + '</span>&nbsp;条&nbsp;-&nbsp;第&nbsp;<span class="'+themeName+'_pgEndRecord">' + (endRecord+gpStartRecord-1) + '</span>&nbsp;条记录');
                  $(mask).remove();
                  valStartRecord = $(t_bar+" ."+themeName+"_pgStartRecord");
                  valEndRecord =$(t_bar+" ."+themeName+"_pgEndRecord");
          }
 		  //获得远程数据
          function getRemoteData(){
                  startLoad();//显示进度条
                  $.ajax(
                          {
	                          type: "POST",
	                          url: proxyUrl + "?startrecord="+gpStartRecord+"&endrecord="+gpEndRecord+"&perpage="+perPage ,
	                          cache: false,
	                          data: ajaxParam,
	                          dataType: "json",
	                          timeout: 30000,
	                          success: function(data){  
	                          		dataStore=data.list;
	                          		totalRecord=data.totalRecord;	                          		
	                          		totalPage = Math.ceil(totalRecord/perPage);  
                                    getStartEnd();//获得当前页开始结束记录	
                                    loadData();//刷新数据容器
                                    refresh();//刷新工具栏状态
                                    overLoad();//置为结束检索状态
	                          },
	                          error: function(){
	                                  alert("请求失败或超时，请稍后再试！");
	                                  overLoad();
	                                  return;
	                          }
                          }
                  );
          }
         // 获得当前页开始结束记录
          function getStartEnd(){
               if(groupSize){
                       startRecord = (currentPage-1)*perPage+1 - gpStartRecord + 1;
                       endRecord = Math.min(currentPage*perPage,totalRecord) - gpStartRecord + 1;
               }else{
                       startRecord = (currentPage-1)*perPage+1;
                       endRecord = Math.min(currentPage*perPage,totalRecord);
               }
          }
          //获得当前组开始结束页码
          function getGroupStartEnd(){
               if(groupSize==null)
                       return;
               var groupId = Math.ceil(currentPage/groupSize);
               gpStartPage = (groupId-1)*groupSize+1;
               gpEndPage = Math.min(groupId*groupSize,totalPage);
               gpStartRecord = (gpStartPage-1)*perPage+1;
               gpEndRecord = Math.min(gpEndPage*perPage,totalRecord);
          }
         
          //移除按钮disabled状态样式
          function enabled(){
                  btnNext.removeClass(themeName+"_pgNextDisabled");
                  btnPrev.removeClass(themeName+"_pgPrevDisabled");
                  btnFirst.removeClass(themeName+"_pgFirstDisabled");
                  btnLast.removeClass(themeName+"_pgLastDisabled");
          }
          // 添加按钮disabled状态样式
          function disabled(){
                  btnNext.addClass(themeName+"_pgNextDisabled");
                  btnPrev.addClass(themeName+"_pgPrevDisabled");
                  btnFirst.addClass(themeName+"_pgFirstDisabled");
                  btnLast.addClass(themeName+"_pgLastDisabled");
          }
          //添加按钮按下状态样式
          function pressHandler(){
                 $(this).addClass(themeName+"_pgPress");
          }
          // 移除按钮按下状态样式
          function unpressHandler(){
                 $(this).removeClass(themeName+"_pgPress");
          }
           //刷新数据容器
          function loadData(){ 
	  			var view = "";     	
				for(var i =startRecord-1;i<endRecord;i++)
				{
					if(dataStore[i]==null || dataStore[i]=='undefined'){
	                  return;
					}else{
					  var item=dataStore[i];					  				  
					  if(hasCheck){
					  	view+="<tr onmouseover=\"setCurrentRow(this)\" onclick=\"setChecked('recCheck','recCheck_"+item[keyField]+"')\">\n";
					  	view+="<td nowrap=\"nowrap\"><input type=\"checkbox\" name=\"recCheck_"+item[keyField]+"\" value=\""+item[keyField]+"\" class=\"checkbox\" onclick=\"setChecked('recCheck','recCheck_"+item[keyField]+"')\"/></td>";	
					  }else{
					  	view+="<tr onmouseover=\"setCurrentRow(this)\">\n";
					  }	
					  for(var k=0;k<cols.length;k++){					  	  
						  $.each(item, function(key, val){						  						  	 	
						  	 	if(key==cols[k]){
						  	 		if(item[cols[k]]==""){
						  	 			view+="<td nowrap=\"nowrap\">&nbsp;</td>\n";
						  	 		}else{						  	 			
						  	 			if(picCol!=null && cols[k]==picCol[0] && item[picCol[1]]=="1" ){						  	 			
					  	  					view+="<td nowrap=\"nowrap\">"+item[cols[k]]+"<font color='red'>"+picCol[2]+"</font></td>\n";				
					  	  				}else{
					  	  					view+="<td nowrap=\"nowrap\">"+item[cols[k]]+"</td>\n";
					  	  				}					  	 			
						  	 		}
						  	 		return ;
						  	 	}
							});  
						}											 
						if(isView){
							view+="<td><a class=\"blo\" href=\"javascript:view('"+item[keyField]+"')\">详细</a></td>\n"
						}
 					    view+="</tr>\n";  
					}				  
				}	
				//如果不够一页，补空行				
				if(endRecord-startRecord<perPage){
					var strBlankTr=getBlankTr(perPage-endRecord+startRecord-1);	   
					view+=strBlankTr; 		
				}         		   
	            valContainer.html(view);
          }
          
          // 刷新工具栏状态
          function refresh(){ 
                  valCurrentPage.val(currentPage);
                  valStartRecord.html(startRecord+gpStartRecord-1);
                  valEndRecord.html(endRecord+gpStartRecord-1);
                  valTotalPage.html(totalPage);
                 
                  btn.unbind("mousedown",pressHandler);
                  btn.bind("mouseup",unpressHandler);
                  btn.bind("mouseout",unpressHandler);
                  if(currentPage == 1 && currentPage != totalPage){
                          enabled();
                          btnGo.bind("mousedown",pressHandler);
                          btnPrev.addClass(themeName+"_pgPrevDisabled");
                          btnFirst.addClass(themeName+"_pgFirstDisabled");
                  }else if(currentPage != 1 && currentPage == totalPage){
                          enabled();
                          btnBack.bind("mousedown",pressHandler);
                          btnNext.addClass(themeName+"_pgNextDisabled");
                          btnLast.addClass(themeName+"_pgLastDisabled");
                  }else if(currentPage == 1 && currentPage == totalPage){
                          disabled();
                  }else{
                          enabled();
                          btnBack.bind("mousedown",pressHandler);
                          btnGo.bind("mousedown",pressHandler);
                          btnNext.addClass(themeName+"_pgNext");
                          btnPrev.addClass(themeName+"_pgPrev");
                          btnFirst.addClass(themeName+"_pgFirst");
                          btnLast.addClass(themeName+"_pgLast");
                  }
          }
          
          function getBlankTr(num){
              var strBlankTr="";
	          var blankTr="<tr>";
	          if(hasCheck){
				blankTr+="<td>&nbsp;</td>";	
			  }	
	          for(var j=0;j<cols.length;j++){
	          	blankTr+="<td>&nbsp;</td>";
	          }
	          if(isView){
	          	blankTr+="<td>&nbsp;</td>";
	          }
	          blankTr+="</tr>\n";
	          for(var m=0;m<num;m++){
	          	strBlankTr+=blankTr;
	          }
	          return strBlankTr;
          }
    
     }
}